﻿Partial Public Class _Default
    Inherits System.Web.UI.Page
    Implements View.card



    Private CardPresenter As New Presenter.CardPresenter()

    Private _card As QuizEntities.QuizCard
    Public Property Card() As QuizEntities.QuizCard Implements View.card.Card
        Get
            Return _card
        End Get
        Set(ByVal value As QuizEntities.QuizCard)
            _card = value
        End Set
    End Property


    Private _quiz_Id As String
    Public ReadOnly Property Quiz_Id() As String Implements View.card.Quiz_Id
        Get
            _quiz_Id = quizId.Value
            Return _quiz_Id
        End Get
    End Property

    Private _selectedAnswer As Integer
    Public ReadOnly Property SelectedAnswer() As Integer Implements View.card.SelectedAnswer
        Get
            _selectedAnswer = RBLQuestionCard.SelectedIndex
            _selectedAnswer = _selectedAnswer + 1
            Return _selectedAnswer
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack Then

        Else

            'Card = CardPresenter.GetFirstCard(PreviousPage.QuizId)
            Card = CardPresenter.GetFirstCard(Request.QueryString("qid"))
            ShowQuestionCard(Card)
        End If
    End Sub


   

    Public Sub ShowQuestionCard(ByVal quizCard As QuizEntities.QuizCard) Implements View.card.ShowQuestionCard
        lblQuestion.Text = quizCard.QusestionCard.Ques
        RBLQuestionCard.Items.Clear()
        RBLQuestionCard.Items.Add(quizCard.QusestionCard.OptionOne)
        RBLQuestionCard.Items.Add(quizCard.QusestionCard.OptionTwo)
        RBLQuestionCard.Items.Add(quizCard.QusestionCard.OptionThree)
        RBLQuestionCard.Items.Add(quizCard.QusestionCard.OptionFour)
        quizId.Value = quizCard.QuizUniqueId
        lblTotalQuestion.Text = quizCard.QuestionCount
        'Check.Value = quizCard.QusestionCard.CorrectAnswer
        lblCurrentCardNo.Text = quizCard.Status.CurrentQuestion
        lblIsAnswered.Text = quizCard.Status.IsAnswered
        lblIsCorrectAnswered.Text = quizCard.Status.IsCorrectAnswered
        'lblScore.Text = GetScore(quizCard) 'quizCard.Status.Score
        lblCorrectAnswerCount.Text = quizCard.Status.CorrectQuestionCount

    End Sub

    Private Function TranslateAnswerBooleanToText(ByVal IsCorrect As Boolean)
        If IsCorrect Then
            AnswerBar.Attributes.CssStyle.Add("Background", "#00FF00")
            Return "Correct Answer"
        Else
            AnswerBar.Attributes.CssStyle.Add("Background", "#FF0000")
            Return "Incorrect Answer"
        End If
    End Function

    Private Function GetScore(ByVal quizCard As QuizEntities.QuizCard)
        Return ((CInt(quizCard.Status.CorrectQuestionCount) / CInt(quizCard.Status.CurrentQuestion)) * 100).ToString()
    End Function

    Public Sub showAnswer(ByVal quizCard As QuizEntities.QuizCard) Implements View.card.showAnswer
        lblIsCorrectAnswered.Text = quizCard.Status.IsCorrectAnswered
        lblCorrectAnswerCount.Text = quizCard.Status.CorrectQuestionCount
        lblScore.Text = GetScore(quizCard)
        lblIsAnswered.Text = quizCard.Status.IsAnswered
        lblAnswerText.Text = TranslateAnswerBooleanToText(quizCard.Status.IsCorrectAnswered)
    End Sub

    Public Sub ShowResults(ByVal quizCard As QuizEntities.QuizCard) Implements View.card.ShowResults
        lblQuestion.Text = "Thanks"
        lblCurrentCardNo.Text = quizCard.Status.CurrentQuestion
        lblIsAnswered.Text = quizCard.Status.IsAnswered
        lblIsCorrectAnswered.Text = quizCard.Status.IsCorrectAnswered
        lblScore.Text = GetScore(quizCard) 'quizCard.Status.Score
        lblCorrectAnswerCount.Text = quizCard.Status.CorrectQuestionCount
        RBLQuestionCard.Visible = False
        btnNext.Visible = False
    End Sub

    Protected Sub RBLQuestionCard_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RBLQuestionCard.SelectedIndexChanged
        RBLQuestionCard.Enabled = False
        FillQuizCardDetailsFromPage()
        CardPresenter.Validate_Handler(Page)
    End Sub

    Sub FillQuizCardDetailsFromPage()
        Card = New QuizEntities.QuizCard()
        Card.Status.CurrentQuestion = lblCurrentCardNo.Text
        Card.Status.IsAnswered = lblIsAnswered.Text
        Card.Status.IsCorrectAnswered = lblIsCorrectAnswered.Text
        Card.Status.CorrectQuestionCount = lblCorrectAnswerCount.Text
        Card.QuizUniqueId = quizId.Value

    End Sub

    Private Sub ClearUIFromAnswerChanges()
        AnswerBar.Attributes.CssStyle.Clear()
        lblAnswerText.Text = ""
    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
        ClearUIFromAnswerChanges()
        FillQuizCardDetailsFromPage()
        If Not Card.Status.IsAnswered Then
            Exit Sub
        End If
        RBLQuestionCard.Enabled = True
        CardPresenter.Submit_Handler(Me)
    End Sub

    'Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
    '    'RBLQuestionCard.Enabled = False
    '    'FillQuizCardDetailsFromPage()
    '    'ValidateAnswer(Card)
    'End Sub
End Class